package art.client.views;

import java.util.HashMap;
import java.util.Map;

import art.client.ClientFactory;
import art.shared.entities.LoginInfo;
import art.shared.entities.UserShared;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

public class UserInfoView extends Composite implements IsWidget {

	private UserShared user = null;
	private VerticalPanel panel = new VerticalPanel();
	private Map<String, Widget> widgets = new HashMap<String, Widget>();
	private Button saveButton;
	private Button cancelButton;

	public UserInfoView(final ClientFactory factory, LoginInfo loginInfo) {
		panel.add(factory.getMenuView(loginInfo));
		addRow("Name: ", "name", new TextBox());
		addRow("Nickname: ", "nickname", new TextBox());
		TextBox emailBox = new TextBox();
		emailBox.setEnabled(false);
		addRow("Email: ", "email", emailBox);
		ListBox dropDown = new ListBox();
		dropDown.setVisibleItemCount(1);
		dropDown.addItem("Choose your gender");
		dropDown.addItem("Female");
		dropDown.addItem("Male");
		dropDown.setSelectedIndex(0);
		addRow("Gender: ", "gender", dropDown);
		DateBox birthDateBox = new DateBox();
		birthDateBox.setFormat(new DateBox.DefaultFormat(DateTimeFormat
				.getFormat("yyyy-MM-dd")));
		addRow("Date of Birth: ", "dateOfBirth", birthDateBox);
		addButtons();
		initWidget(panel);
	}

	private void addButtons() {
		HorizontalPanel buttonPanel = new HorizontalPanel();
		saveButton = new Button("Save");
		saveButton.getElement().setId("userinfo-savebutton");
		cancelButton = new Button("Cancel");
		cancelButton.getElement().setId("userinfo-cancelbutton");
		buttonPanel.add(saveButton);
		buttonPanel.add(cancelButton);
		panel.add(buttonPanel);
	}

	public Button getSaveButton() {
		return saveButton;
	}

	public Button getCancelButton() {
		return cancelButton;
	}

	public void insertData(UserShared user) {
		this.user = user;
		((TextBox) widgets.get("name")).setText(user.getName());
		((TextBox) widgets.get("nickname")).setText(user.getNickname());
		((TextBox) widgets.get("email")).setText(user.getEmail());
		if (user.getGender() == null) {
			((ListBox) widgets.get("gender")).setSelectedIndex(0);
		} else if (user.getGender().equals(UserShared.Gender.FEMALE)) {
			((ListBox) widgets.get("gender")).setSelectedIndex(1);
		} else if (user.getGender().equals(UserShared.Gender.MALE)) {
			((ListBox) widgets.get("gender")).setSelectedIndex(2);
		}
		((DateBox) widgets.get("dateOfBirth")).setValue(user.getDateOfBirth());
	}

	private void addRow(String labelText, String id, Widget widget) {
		HorizontalPanel rowPanel = new HorizontalPanel();
		rowPanel.addStyleName("userinforow");
		Label label = new Label(labelText);
		label.getElement().setId("userinforow-label-"+id);
		rowPanel.add(label);
		rowPanel.add(widget);
		widget.getElement().setId("userinforow-widget-"+id);
		panel.add(rowPanel);
		widgets.put(id, widget);
	}

	public Map<String, Widget> getWidgets() {
		return widgets;
	}
	
	public UserShared getUser() {
		return user;
	}

}
